<?php


namespace App\Console\Commands\Cruise;


use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseTemplateModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class FixCruiseOrderStatusHistoryRecord extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:fix-cruise-order-status {--id=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '门店自检:修复历史单状态';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $id         = $this->option('id');
        $allChanges = [];
        CruiseOrderModel::query()
                        ->when($id, function ($query) use ($id) {
                            $query->where('id', $id);
                        })
                        ->where('type', CruiseTemplateModel::TYPE_8)
                        ->whereIn('status', [CruiseOrderModel::STATUS_NOT_MODIFY, CruiseOrderModel::STATUS_COMPLETE])
                        ->chunkById(500, function ($modelList) use (&$allChanges) {
                            /** @var CruiseOrderModel $cruiseOrder */
                            foreach ($modelList as $cruiseOrder) {
                                $info = vsprintf("cruiseOrder变更--> id:%s;变更前:%s;变更为:%s", [
                                    $cruiseOrder->id,
                                    $cruiseOrder->status,
                                    CruiseOrderModel::STATUS_NOT_REVIEW
                                ]);
                                Log::info($info);
                                $this->info($info);
                                $allChanges[$cruiseOrder->id] = [
                                    'id'     => $cruiseOrder->id,
                                    'status' => $cruiseOrder->status
                                ];
                                $cruiseOrder->status          = CruiseOrderModel::STATUS_NOT_REVIEW;
                                $cruiseOrder->save();
                            }
                        });

        Log::info('cruiseOrder所有变更', array_values($allChanges));
    }

}